package com.wanmait.hotelManagement.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wanmait.hotelManagement.util.DBHelper;
import com.wanmait.hotelManagement.vo.Picture;
import com.wanmait.hotelManagement.vo.RoomType;

public class AdminRoomTypeDao
{
	/*
	 * public static void main(String[] args) { System.out.println("OK"); }
	 */
	
	private AdminRoomTypeDao() {}
	private static AdminRoomTypeDao adminRoomTypeDao = new AdminRoomTypeDao();
	public static AdminRoomTypeDao getDao()
	{
		return adminRoomTypeDao;
	}
	
	public void addRoomType(RoomType roomType)
	{
		String sql = "insert into room_type(room_name,room_price,person_count,room_count,remarks) values (?,?,?,?,?)";
		DBHelper.getDbHelper().update(sql, roomType.getRoomName(), roomType.getRoomPrice(), roomType.getPersonCount(), roomType.getRoomCount(), roomType.getRemarks());
		
	}
	
	public List<RoomType> getAllRoomType()
	{
		List<RoomType> allRoomType = new ArrayList<RoomType>();
		Connection conn = DBHelper.getDbHelper().getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM room_type,picture WHERE picture.room_type_id=room_type.id";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			RoomType prevRoomType = null;
			List<Picture> pictures = null;
			while(rs.next())
			{
				if(prevRoomType==null||prevRoomType.getId()!=rs.getInt("room_type.id"))
				{
					RoomType roomType = new RoomType();
					roomType.setId(rs.getInt("room_type.id"));
					roomType.setRoomName(rs.getString("room_name"));
					roomType.setRoomPrice(rs.getFloat("room_price"));
					roomType.setPersonCount(rs.getInt("person_count"));
					roomType.setRoomCount(rs.getInt("room_count"));
					roomType.setRemarks(rs.getString("remarks"));
					pictures = new ArrayList<Picture>();
					roomType.setPictures(pictures);
					allRoomType.add(roomType);
					prevRoomType = roomType;
				}
				Picture picture = new Picture();
				picture.setImages(rs.getString("images"));
				pictures.add(picture);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBHelper.getDbHelper().close(rs, ps, conn);
		}
		return allRoomType;
	}
}
